package jy.util;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 * 字符编码过滤器，统一request和response的编码
 * 
 * @author LIUQ
 *
 */
public class EncodingFilter extends HttpServlet implements Filter {
	/* 配置信息 */
	private FilterConfig config;
	/* 采用的字符集 */
	private String endcoding = "";
	private static final long serialVersionUID = 1L;
	/**
	 * 从web.xml中读取配置信息，初始化过滤器
	 */
	public void init(FilterConfig config) throws ServletException{
		this.config = config;
		/* 取到字符集 */
		this.endcoding = this.config.getInitParameter("encoding");
	}
	/* 设置request和response的字符集，在web.xml中配置字符集为utf-8 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
	throws java.io.IOException, ServletException{		
		
		request.setCharacterEncoding(this.endcoding);
		response.setCharacterEncoding(this.endcoding);
		chain.doFilter(request, response);
	}
}
